/*
 * @Author: jianglijun 
 * @Date: 2019-02-23 23:22:08 
 * @Last Modified by: jianglijun
 * @Last Modified time: 2019-03-01 00:24:28
 */
var express = require('express');
var router = express.Router();
var mongoose = require('mongoose');
var goods = require("../models/goods");
var _ = require('lodash');
var User = require("../models/user");
var DB_URL = 'mongodb://localhost:27017/shop';

mongoose.connect(DB_URL);
/**
  * 连接成功
  */
 mongoose.connection.on('connected', function () {    
    console.log('Mongoose connection open to ' + DB_URL);  
});    

/**
 * 连接异常
 */
mongoose.connection.on('error',function (err) {    
    console.log('Mongoose connection error: ' + err);  
});    
 
/**
 * 连接断开
 */
mongoose.connection.on('disconnected', function () {    
    console.log('Mongoose connection disconnected');  
});    

/* GET users listing. */
// ------------- 商品列表  ----------  //
router.get('/list', function(req, res, next) {
    let page = +req.param("page") || 1;
    let pagSize = +(req.param("pageSize") || 5);
    let sort = +req.param("sort") || 1;
    let minPrice = +req.param("minPrice") ;
    let maxPrice = +req.param("maxPrice") ;
    let skip = (page-1)*pagSize;
    let param = {};
    if(minPrice==0 && maxPrice==0){
        param = {};
    }else{
        param = {
            "salePrice":{$lte:maxPrice,$gte:minPrice}
        }
    }
    goods.find(param).limit(pagSize).skip(skip)
    .sort({"salePrice":sort})
    .exec((err,doc) => {
        if(err){
            res.json({
                errno:1000,
                msg:err.message
            })
        }else {
            res.json({
                errno:0,
                msg:"成功",
                data:{
                    count:doc.length,
                    list:doc
                }
            })
        }
    })
});
//  ---------- 添加到购物车  -----------//
router.get("/addCart",(req,res,next) => {
    let userId = "100000077",
        productId = req.param("productId");
    User.findOne({userId},(err,doc) => {
        if(err){
            res.json({
                errno:1001,
                msg:err.message
            })
        }
        else {
            let gooditem ="";
            doc.cartList.forEach(ele => {
                if(ele.productId==productId){
                    gooditem = ele;
                    ele.productNum++;
                }
            });
            if(!gooditem){
                goods.findOne({productId},(err,doc1) => {
                    if(err) {
                        res.json({
                            errno:1001,
                            msg:err.message
                        })
                    } 
                    else {
                        if(doc1) {
                            let newObj = {
                                productNum:1,
                                checked:true,
                                productId:doc1.productId,
                                salePrice:doc1.salePrice,
                                productName:doc1.productName,
                                productImage:doc1.productImage,
                                storeNum: doc1.storeNum,
                                productUrl: doc1.productUrl
                            };
                            doc.cartList.push(newObj);
                            doc.save((err,doc2) => { 
                                if(err){
                                    res.json({
                                        errno:1001,
                                        msg:err.message
                                    }) 
                                } 
                                else {
                                    res.json({
                                        errno:0,
                                        msg:"成功",
                                        data:"成功"
                                    })
                                }
                            })
                        }
                    }
                })
            }else{
                doc.save((err,doc2) => { 
                    if(err){
                        res.json({
                            errno:1001,
                            msg:err.message
                        }) 
                    } 
                    else {
                        res.json({
                            errno:0,
                            msg:"添加成功",
                            data:"成功"
                        })
                    }
                })
            }
        }
    })
})
// ------------- 购物车数量 -----------//
router.get("/cartCount",(req,res,next) => {
  if(req.cookies.userId){
    let userId = req.cookies.userId;
    User.findOne({userId},(err,doc) => {
        if(err){
            res.json({
                errno:1,
                msg:err.message
            })
        }
        else {
            if(doc){
                let count = 0;
                doc.cartList.forEach( item => {
                    count += item.productNum
                })
                res.json({
                    errno:0,
                    msg:"成功",
                    data:count
                })
            }
        }
    })
  }    
})
module.exports = router;
